package com.sakura.mall.coupon.seckillSkuRelation.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.coupon.seckillSkuRelation.domain.entity.SeckillSkuRelationEntity;
import com.sakura.mall.coupon.seckillSkuRelation.domain.request.AddSeckillSkuRelationRequest;
import com.sakura.mall.coupon.seckillSkuRelation.domain.request.UpdateSeckillSkuRelationRequest;
import com.sakura.mall.coupon.seckillSkuRelation.domain.request.SeckillSkuRelationPageRequest;
import com.sakura.mall.coupon.seckillSkuRelation.domain.vo.SeckillSkuRelationVo;
import com.sakura.mall.coupon.seckillSkuRelation.mapper.SeckillSkuRelationMapper;
import com.sakura.mall.coupon.seckillSkuRelation.service.SeckillSkuRelationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 秒杀活动商品关联 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class SeckillSkuRelationServiceImpl extends ServiceImpl<SeckillSkuRelationMapper, SeckillSkuRelationEntity> implements SeckillSkuRelationService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddSeckillSkuRelationRequest request) {
        // todo 其他校验逻辑
        SeckillSkuRelationEntity seckillSkuRelation = request.convert(SeckillSkuRelationEntity.class);
        this.save(seckillSkuRelation);
    }

    @Override
    public Page<SeckillSkuRelationVo> queryPage(SeckillSkuRelationPageRequest request) {
        IPage<SeckillSkuRelationEntity> qp = request.toPage();
        LambdaQueryWrapper<SeckillSkuRelationEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<SeckillSkuRelationVo> ret = qp.convert(entity -> entity.convert(SeckillSkuRelationVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateSeckillSkuRelationRequest request) {
        SeckillSkuRelationEntity entity = request.convert(SeckillSkuRelationEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

